package com.minivision.fdiot.mqtt.message;

import java.nio.ByteBuffer;

import com.minivision.fdiot.protopool.CodecException;
import com.minivision.fdiot.protopool.BinaryBody;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class UpgradeFile extends BinaryBody {

  private static final long serialVersionUID = 3835330408628608474L;
  
  //文件功能
  private int func;
  //文件
  private byte[] file;

  @Override
  public byte[] encode() throws CodecException {
    ByteBuffer buffer = ByteBuffer.allocate(4 + file.length);
    buffer.putInt(func);
    buffer.put(file);
    return buffer.array();
  }

  @Override
  public BinaryBody decode(byte[] bytes) throws CodecException {
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    this.func = buffer.getInt();
    buffer.position(4);
    file = new byte[buffer.remaining()];
    buffer.get(file);
    return this;
  }

}
